﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using WindowsLive.Writer.Api;

namespace iTunesNowPlaying
{
    public class Settings
    {
        IProperties m_properties;

        public Settings(IProperties properties)
        {
            m_properties = properties;
        }

        public bool ShowArtist
        {
            get { return m_properties.GetBoolean("Artist", true); }
            set { m_properties.SetBoolean("Artist", value); }
        }

        public bool ShowTitle
        {
            get { return m_properties.GetBoolean("Title", true); }
            set { m_properties.SetBoolean("Title", value); }
        }

        public bool ShowAlbum
        {
            get { return m_properties.GetBoolean("Album", false); }
            set { m_properties.SetBoolean("Album", value); }
        }

        public string Prefix
        {
            get { return m_properties.GetString("Prefix", "Whilst writing this, I was listening to"); }
            set { m_properties.SetString("Prefix", value); }
        }

        internal string GetText(bool album, bool artist, bool title, string prefix)
        {
            return GetText(album, artist, title, true, prefix, "%Album%", "%Artist%", "%Title%");            
        }

        internal string GetText(bool album, bool artist, bool title, bool preview, string prefix,
                            string stAlbum, string stArtist, string stTitle)
        {
            StringBuilder st = new StringBuilder();
            if (!preview)
                st.Append("<p>");
            if (!string.IsNullOrEmpty(prefix))
            {
                if (!prefix.EndsWith(" "))
                    prefix += " ";
                st.Append(prefix);
            }
            if (artist)
            {                
                if(!preview)
                    st.Append(string.Format("<a href=\"http://www.rhapsody.com/-search?searchtype=RhapArtist&amp;query={0}\" title=\"Search for {1}\">{1}</a>", HttpUtility.HtmlEncode(stArtist), stArtist));
                else
                    st.Append(stArtist);
            }
            if (title)
            {
                if (artist)
                    st.Append(" - ");
                if (!preview)
                    st.Append(string.Format("<a href=\"http://www.rhapsody.com/-search?searchtype=RhapTrack&amp;query={0}\" title=\"Search for {1}\">{1}</a>", HttpUtility.HtmlEncode(stTitle), stTitle));
                else
                    st.Append(stTitle);
            }
            if (album)
            {
                if (artist || title)
                    st.Append(" - ");
                if (!preview)
                    st.Append(string.Format("<a href=\"http://www.rhapsody.com/-search?searchtype=RhapAlbum&amp;query={0}\" title=\"Search for {1}\">{1}</a>", HttpUtility.HtmlEncode(stAlbum), stAlbum));
                else
                    st.Append(stAlbum);
            }
            if (!preview)
                st.Append("</p>");
            return st.ToString();
        }
    }
}
